// ActionScript file
import flash.events.MouseEvent;

import mx.rpc.events.ResultEvent;

private var _username:String;
private var _password:String;
private var _loggedInToTwitter:Boolean = false;


public function initTwitter()
{
	leftPanelWrapper.twitterLogin.addEventListener(MouseEvent.CLICK, twitterLogin);
	
	twitterRemoteObject.twitterRemote.addEventListener(FaultEvent.FAULT, faultListener);
	twitterRemoteObject.twitterRemote.getOperation('checkCredentials').addEventListener(ResultEvent.RESULT, loginListener);
	twitterRemoteObject.twitterRemote.getOperation('showStatus').addEventListener(ResultEvent.RESULT, tweetListener);
	twitterRemoteObject.twitterRemote.getOperation('postTweet').addEventListener(ResultEvent.RESULT, tweetListener);
	twitterRemoteObject.twitterRemote.getOperation('getTweets').addEventListener(ResultEvent.RESULT, getTweetsListener);
	twitterRemoteObject.twitterRemote.getOperation('deleteTweet').addEventListener(ResultEvent.RESULT, getTweetsListener);
	twitterRemoteObject.twitterRemote.getOperation('getFollowers').addEventListener(ResultEvent.RESULT, getTweetsListener);
}

private function twitterLogin(event:MouseEvent):void
{
	_username = leftPanelWrapper.twitterUsername.text;
	_password = leftPanelWrapper.twitterPassword.text;
	twitterRemoteObject.twitterRemote.checkCredentials(_username, _password);
}

private function showStatus(tweetText:String):void {
	twitterRemoteObject.twitterRemote.showStatus(_username, _password, tweetText);
}

private function postTweet(tweetText:String):void {
	twitterRemoteObject.twitterRemote.postTweet(_username, _password, tweetText);
}

private function deleteTweet(tweetText:String):void {
	twitterRemoteObject.twitterRemote.deleteTweet(_username, _password, tweetText);
}

private function getFollowers():void {
	twitterRemoteObject.twitterRemote.getFollowers(_username, _password);
}

private function getTweets():void {
	twitterRemoteObject.twitterRemote.getTweets(_username, _password);
}

private function loginListener(event:ResultEvent):void
{
	leftPanelWrapper.twitterUsername.enabled = false;
	leftPanelWrapper.twitterPassword.enabled = false;
	leftPanelWrapper.twitterStatus.text      = "Logged in as " + _username;
	leftPanelWrapper.twitterLogin.visible    = false;
	_loggedInToTwitter = true;
	getTweets();
}

private function tweetListener(event:ResultEvent):void {
	getTweets();
}

private function getTweetsListener(event:ResultEvent):void {
	//dataGrid.dataProvider = event.result as Array;
	middlePanelWrapper.dataGrid.dataProvider = event.result as Array;
}